package ui.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.dialog.DialogColumnConfig;
import ui.util.IStructuredViewer;

public class ColumnConfigCommand extends AbstractHandler {
	private final static Logger logger = 
		LoggerFactory.getLogger(ColumnConfigCommand.class);
	
	private TableViewer tableViewer;

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
//		tableViewer = UiUtil.getTableViewer();
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPart part = window.getActivePage().getActivePart();
		
		tableViewer = ((IStructuredViewer)part).getTableViewer();
		if (tableViewer == null){
			MessageDialog.openInformation(window.getShell(), "Info",
					"There are no Columns in the active window");
			return null;
		}
		
		DialogColumnConfig dialog = new DialogColumnConfig(HandlerUtil.getActiveWorkbenchWindow(
				event).getShell(),tableViewer);
		dialog.create();

		if (dialog.open() == Window.OK) {
			logger.debug("zzz:{}",dialog.getTableViewer().getTable().getColumnCount());
			// System.out.println(dialog.getFirstName());
			// System.out.println(dialog.getLastName());
		}

		return null;

	}

}
